<template>
  <div class="login">
    <van-nav-bar left-arrow>
      <template #left>
        <i class="iconfont iconbtn_nav_back iconFont" @click="goBack"></i>
      </template>
    </van-nav-bar>
    <div class="main">
      <h2>你好,请登录</h2>
      <van-form ref="form">
        <van-field
          v-model="form.mobile"
          placeholder="请输入手机号码"
          :rules="rules.mobile"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont iconbianzu1"></i>
          </template>
        </van-field>
        <van-field
          v-model="form.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #button>
            <span class="myColor" @click="getCode">
              {{ isBack ? time + 'S后重新发送' : '获取验证码' }}
            </span>
          </template>
          <template #left-icon>
            <i class="iconfont iconyanzhengma"></i>
          </template>
        </van-field>
        <div class="https">
          登录即同意 <span class="myColor"> 《用户使用协议》</span>和<span
            class="myColor"
          >
            《隐私协议》</span
          >
        </div>
        <div style="margin: 16px;">
          <van-button
            round
            block
            type="danger"
            native-type="submit"
            @click="login"
          >
            确定
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { apigetCode, apiLogin } from '@/api/au.js'
import { localSet } from '@/utils/local.js'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789|1]66|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请正确输入手机号',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '请正确输入验证码',
            trigger: 'onBlur'
          }
        ]
      },
      isBack: false,
      time: 8
    }
  },
  methods: {
    // 获取验证码
    async getCode () {
      try {
        await this.$refs.form.validate('mobile')
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true
        })
        const resCode = await apigetCode(this.form.mobile)
        // 提示验证码
        this.$toast.success(resCode.data)
        if (this.isBack) {
          return
        }
        this.isBack = true
        const timeId = setInterval(() => {
          this.time--
          if (this.time < 0) {
            clearInterval(timeId)
            this.time = 8
            this.isBack = false
          }
        }, 1000)
      } catch (error) {
        this.$toast.fail(error.message)
      }
    },
    // 登录
    async login () {
      this.$toast.loading({ duration: 0 })
      try {
        // 登录请求
        const res = await apiLogin(this.form)
        // 设置图片地址
        res.data.user.avatar = 'http://localhost:1337' + res.data.user.avatar
        // 保存信息
        this.$store.commit('setUserInfo', res.data.user)
        // 保存token
        localSet('token', res.data.jwt)
        console.log(res)
        this.$toast.success('登录成功')
        // 跳转页面
        const redirect = this.$route.query._redirect
        // console.log('路由拼接参数', redirect)
        if (redirect) {
          this.$router.push(redirect)
        } else {
          this.$router.push('/home')
        }
      } catch (error) {
        // console.log(error)
        this.$toast.fail(error.message)
      }
    },
    // 回退
    goBack () {
      const redaiect = this.$route.query._redirect
      if (redaiect) {
        this.$router.push('/home/find')
      } else {
        this.$router.go(-1)
      }
    }
  }
}
</script>

<style lang="less">
.login {
  font-size: 12px;
  .iconFont {
    font-size: 44px;
  }
  .van-nav-bar__left {
    padding-left: 0;
  }
  .main {
    padding: 0 15px;
    h2 {
      margin: 33px 0 44px 0;
    }
  }
  .myColor {
    color: #00b8d4;
  }
  .https {
    margin-top: 20px;
    margin-bottom: 40px;
  }
}
</style>
